<?PHP
require_once WACT_ROOT.'controller/controller.inc.php';
require_once WACT_ROOT.'controller/form.inc.php';
require_once WACT_ROOT. 'util/dataspace.inc.php';
require_once APP_ROOT.'view/common/layout.formview.php';

require_once APP_ROOT.'model/common/user.php';
require_once APP_ROOT.'model/common/categories.php';
require_once APP_ROOT.'model/common/session.php';
require_once APP_ROOT.'model/common/article.php';
require_once 'model/common/date.php';
require_once APP_ROOT . "model/common/site.php";

class ArticlesIndexView extends LayoutFormView {
	function ArticlesIndexView ($TemplateFile){
		parent::LayoutView($TemplateFile);

	}

	function prepare(&$controller, &$request, &$responseModel){
		parent::prepare();
		if(!Session::isLogin())
			$this->Template->set('login',' ');
		$docbase="http://".$_SERVER['HTTP_HOST'];
		$this->Template->set('docbase',$docbase);

		if(APP_NAME!=""){
			$this->Template->set('app_name',APP_NAME);
		} else $this->Template->set('app_name','.');

               
		$this->Template->set('SITE_CSS',Site::getSiteTemplateCSS());
		$this->Template->set('SITE_BASE',Site::getSiteTemplateBase());
		$this->Template->set('SITE_TITLE',Site::getTitle());
		$this->Template->set('title',Site::getTitle().' - Story index');

 
		if(!Session::isLogin())
			$this->Template->set('login',' ');

		//load filters
		Session::loadResponseModel($responseModel,'filter.articles');

		$user_id=$request->getParameter('user_id');
		if($user_id){
			$responseModel->set('author',$user_id);
		}
		//article list
		$pager=& $this->Template->getChild('pagenav');
		if($responseModel->get('item'))
			$pager->Items=$responseModel->get('item');
		$list=Article::getArticlesList($pager,$request,$responseModel,true);
		$this->Template->setChildDataSource('articles',$list);


		if($responseModel->get('type_recent')=='1'){
			$responseModel->set('type_recent','1');
		}else $responseModel->set('type_recent','2');


		//populate recent combo
        $recent =& $this->Template->getChild('recent');
        $recent_list  =& Article::getResentList();
        $recent->setChoices($recent_list);
        $valrecent=$responseModel->get('recent');
        $recent->setSelection($valrecent);

		//populate categories
        $categories1 =& CategoriesModel::getSportCategories(false,false,true);
        $categories2 =& CategoriesModel::getCommunityCategories(false,false,true);
        $category1 =& $this->Template->getChild('category1');
        $category2 =& $this->Template->getChild('category2');
        $category1->setChoices($categories1);
        $valcategory1=$responseModel->get('category1');
        $category1->setSelection($valcategory1);
        $category2->setChoices($categories2);
        $valcategory2=$responseModel->get('category2');
        $category2->setSelection($valcategory2);

        //populate authors
        $authors_list =& User::getAuthorsList();
        $author =& $this->Template->getChild('author');
        $author->setChoices($authors_list);
        $valauthor=$responseModel->get('author');
        $author->setSelection($valauthor);

         //populate months
        $start_month =& $this->Template->getChild('start_month');
        $months_list =& SportDate::getMonthsList();
        $start_month->setChoices($months_list);
        $valmonth=$responseModel->get('start_month');
        $start_month->setSelection($valmonth);

         //prepopulate start and end date
        $cur_month = (integer)date("m");
        $cur_year = (integer)date("Y");
        $cur_day = (integer)date("d");

        if ( $cur_month==1 ) {
        	$prev_month = 12;
        	$prev_year = $cur_year-1;
        }  else {
           $prev_month = $cur_month - 1;
           $prev_year = $cur_year;
        }

        if ( $cur_month==12 ) {
        	$next_month = 1;
        	$next_year = $cur_year+1;
        }  else {
           $next_month = $cur_month + 1;
           $next_year = $cur_year;
        }

        $start_day =& $this->Template->getChild('start_day');
        $day = $start_day->getValue();
        if (!$day) $start_day->setValue(1);

        /*$end_day =& $this->Template->getChild('end_day');$day = $end_day->getValue();if (!$day) $end_day->setValue($cur_day);*/

        //print $start_month->getValue();
        if ($start_month->getValue()<=0) $start_month->setSelection($prev_month);
        /*if ($end_month->getValue()<=0) {//print $cur_month;$end_month->setSelection($cur_month);}*/

        $start_year =& $this->Template->getChild('start_year');
        $year = $start_year->getValue();
        if (!$year) $start_year->setValue($prev_year);

		//item per page
		$item=&$this->Template->getChild('item');
		$item_list=array("10"=>"10","20"=>"20","25"=>"25","50"=>"50","100"=>"100");
		$item->setChoices($item_list);


	}
}

class ArticlesIndexPage extends PageController {
	function ArticlesIndexPage() {
		parent::PageController();

		$Form=new FormController();
		$Form->addChild('list',new ButtonController(new Delegate($this, 'articlelist')));
		$Form->setDefaultChild('list');
		$this->addChild('FilterForm',$Form);
		$this->setDefaultChild('FilterForm');

        $mytemplate = 'pub/articles/index.html';
		$this->setDefaultView(new ArticlesIndexView($mytemplate));


	}

	function articlelist(&$controller,&$request,&$responseModel){
		Session::saveResponseModel($responseModel,'filter.articles');

	}
}
?>
